﻿Imports System.Data.OleDb, System.Drawing
Public Class editProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
            Dim SqlString As String = "Select * From Profile Where VisitorId = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_FirstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_LastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_Description.Text = dr("Description")
            End If
        End If
    End Sub

    Protected Sub btn_SaveProfile_Click(sender As Object, e As EventArgs) Handles btn_SaveProfile.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("travelConnectionString").ConnectionString)
        If fup_Picture.HasFile Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() + ".jpg"
            fup_Picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
            SqlString = "Update Profile Set FirstName = @f1, LastName = @f2, Description = @f3, Picture = @f4 Where VisitorId = @f5"
        Else
            SqlString = "Update Profile Set FirstName = @f1, LastName = @f2, Description = @f3 Where VisitorId = @f5"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        If fup_Picture.HasFile Then cmd.Parameters.AddWithValue("@f4", "pictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("profile.aspx?VisitorId=" & User.Identity.Name)
    End Sub
End Class